# -*- coding: utf-8 -*-
"""
summary:
    此类是实现在Template里面方便使用 Filter 来调用一些通用的信息
    避免重视在Views里面反复的读信息传到Template里面
author:
    Jason Lee <huacnlee@gmail.com>
"""
from django.template import Library,defaultfilters
from settings import *


register = Library()

def app_name():
    """
    summary:
        项目名称
    """
    return APP_NAME

def app_domain(url=''):
    return APP_DOMAIN + url

def _get_face_url(face,width=0,height=0,mode='flickr'):
    """
    summary:
        返回用户头像地址
    """
    if face == '':
        face = DEFAULT_FACE
        
    return "%s?width=%d&height=%d&mode=%s" % (face,width,height,mode)

def face75(face,mode='flickr'):
    return _get_face_url(face, 75, 75,mode)

def face24(face,mode='flickr'):
    return _get_face_url(face, 24, 24,mode)

def include_js(filepath):
    """
    summary:
        <script>生成，并控制全局JS版本控制,以更新浏览器长缓存
    author:
        Jason Lee <huacnlee@gmail.com>
    """
    format = '<script type="text/javascript" src="/static/scripts/%s?v=%s"></script>'
    result = format % (filepath,APP_VERSION)
    return defaultfilters.safe(result)

def include_css(filepath,media='screen'):
    """
    summary:
        <link>生成，并控制全局css版本控制,以更新浏览器长缓存
    author:
        Jason Lee <huacnlee@gmail.com>
    """
    return defaultfilters.safe('<link type="text/css" rel="stylesheet" href="/static/styles/%s?v=%s" media="%s" />' % (filepath,APP_VERSION,media))


def format(value, arg):
    """
    Alters default filter "stringformat" to not add the % at the front,
    so the variable can be placed anywhere in the string.
    """
    try:
        if value:
            return (unicode(arg)) % value
        else:
            return u''
    except (ValueError, TypeError):
        return u''
    


register.simple_tag(app_domain)
register.simple_tag(app_name)

register.filter('face24', face24)
register.filter('face75', face75)

register.filter('include_js', include_js)
register.filter('include_css', include_css)

register.filter('format', format)
